/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.server.config;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.annotation.Order;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;


/**
 * Admin application config
 * 
 * https://blog.42.nl/articles/drop-xml-configure-your-spring-web-application-in-java/
 * http://www.robinhowlett.com/blog/2013/02/13/spring-app-migration-from-xml-to-java-based-config/
 * http://stackoverflow.com/questions/14206564/spring-mvc-two-instances-of-configuration-instead-of-one
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
@Configuration
@ComponentScan(basePackages =
{ "com.daikit" }, excludeFilters =
{ @Filter(Configuration.class) })
@PropertySource(
{ "classpath:/application.properties" })
public class ApplicationConfig
{

	@Autowired
	private ApplicationContext applicationContext;

	/**
	 * @return the message source
	 */
	@Bean(name = "messageSource")
	@Order(value = 10)
	public AbstractMessageSource getMessageSource()
	{
		final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setDefaultEncoding("UTF-8");
		messageSource.setFallbackToSystemLocale(false);
		messageSource.setBasenames(new String[]
		{ "classpath:messages" });
		return messageSource;
	}

	/**
	 * @return the locale resolver
	 */
	@Bean(name = "localeResolver")
	public LocaleResolver getLocaleResolver()
	{
		final CookieLocaleResolver localeResolver = new CookieLocaleResolver();
		localeResolver.setCookieName("clientlanguage");
		// In seconds. If set to -1, the cookie is not persisted (deleted when browser shuts down)
		localeResolver.setCookieMaxAge(Integer.valueOf(1000000));
		return localeResolver;
	}

	/**
	 * Lower priority (order is higher) than message source to have message source bean created before
	 * 
	 * @return the validator
	 */
	@Bean(name = "validator")
	@Order(value = 11)
	public Validator getJSR303Validator()
	{
		final LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(applicationContext.getBean(AbstractMessageSource.class));
		return validator;
	}
}
